package com.sanjiahantang.youjiaclient.module.me.order.views.impl;

import android.app.Dialog;
import android.content.Intent;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.youjiaclient.R;
import com.sanjiahantang.youjiaclient.base.BaseMvpActivity;
import com.sanjiahantang.youjiaclient.base.BaseView;
import com.sanjiahantang.youjiaclient.config.ConfigValue;
import com.sanjiahantang.youjiaclient.module.common.views.impl.WebActivity;
import com.sanjiahantang.youjiaclient.module.me.contract.beans.ContractSaveRequest;
import com.sanjiahantang.youjiaclient.module.me.order.beans.CreateServiceOrderResponseBean;
import com.sanjiahantang.youjiaclient.module.me.order.beans.RequireOrderBean;
import com.sanjiahantang.youjiaclient.module.me.order.beans.Service3PartBean;
import com.sanjiahantang.youjiaclient.module.me.order.presenters.OrderPresenter;
import com.sanjiahantang.youjiaclient.module.me.order.views.OrderView;
import com.sanjiahantang.youjiaclient.module.me.require.beans.RequireListBean;
import com.sanjiahantang.youjiaclient.module.me.require.views.impl.RequireOrderDetailActivity;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by zy on 2017/10/24.
 */

public class OrderConfirmActivity extends BaseMvpActivity<OrderView, OrderPresenter> implements OrderView, View.OnClickListener {


    private int result;
    private TextView txtPrice, txtTotal, txtCount, txtSubmit;
    private RequireOrderBean.Data data;
    private Service3PartBean.Data.OrderServiceRaDtoListBean responseData;
    private CheckBox checkBox;
    private RequireListBean.Item requireData;
    private boolean canCheck = false;
    private View txtContract,txtContractSigned;

    @Override
    public void showProgress() {
        super.showProgress($this);
    }

    @Override
    public OrderPresenter initPresenter() {
        return new OrderPresenter();
    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_require_order);
        TextView title = (TextView) findViewById(R.id.title);
        title.setText("确认订单");
        data = (RequireOrderBean.Data) getIntent().getSerializableExtra(KEY_PARAM);
        requireData = (RequireListBean.Item) getIntent().getSerializableExtra("requireData");
        findViewById(R.id.fl_Left).setOnClickListener(this);
        txtPrice = (TextView) findViewById(R.id.txtPrice);
        txtTotal = (TextView) findViewById(R.id.txtTotal);
        txtCount = (TextView) findViewById(R.id.txtCount);
        findViewById(R.id.txtSubmit).setOnClickListener(this);
        checkBox = (CheckBox) findViewById(R.id.checkBox);
        txtContract = findViewById(R.id.txtContract);
        txtContractSigned = findViewById(R.id.txtContractSigned);
        txtContract.setOnClickListener(this);
        if (data.getOrderCode() == null) {
            price = Double.parseDouble(data.getServicePrice());
            num = data.getServiceNum();
            txtPrice.setText(" ¥ " + String.format("%.2f", price * num));
            txtTotal.setText(String.format("%.2f", price * num));
            result = (int) (price * num);
        }

        if ("1".equals(data.getStatus())){
            checkBox.setChecked(true);
            canCheck = true;
            txtContractSigned.setVisibility(View.VISIBLE);
            txtContract.setVisibility(View.GONE);
        }else {
            checkBox.setChecked(false);
            txtContractSigned.setVisibility(View.GONE);
            txtContract.setVisibility(View.VISIBLE);
        }

        checkBox.setOnClickListener(this);
    }

    double price;
    int num;

    @Override
    public void initData() {

        if (data.getOrderCode() != null) {
            presenter.searchServiceRaForCode($this, data.getOrderCode(), new BaseView.OnLoadListener() {
                @Override
                public void onLoad(Object content) {
                    responseData = (Service3PartBean.Data.OrderServiceRaDtoListBean) content;
                    price = responseData.getServiceRecordList().get(0).getPrice();
                    num = responseData.getServiceRecordList().get(0).getCount();
                    txtPrice.setText(" ¥ " + String.format("%.2f", price * num));
                    txtTotal.setText(String.format("%.2f", price * num));
                    result = (int) (price * num);

                    if (responseData.getServiceRecordList().get(0).getStatusId() == 30) {
                        findViewById(R.id.llBottom).setVisibility(View.GONE);
                    }
                }
            });
        }

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fl_Left:
                setResult(4, getIntent());
                finish();
                break;
            case R.id.txtSubmit:
                if (data.getOrderCode() == null) {
                    if (checkBox.isChecked()) {
                        presenter.createOrderForDesign($this, data.getUserRequireId(), result, data, new BaseView.OnLoadListener() {
                            @Override
                            public void onLoad(Object content) {
                                CreateServiceOrderResponseBean.Data responseData = (CreateServiceOrderResponseBean.Data) content;
                                data.setOrderCode(responseData.getOrderCode());
                                startActivity(new Intent($this, RequireOrderDetailActivity.class)
                                        .putExtra(KEY_PARAM, data));
                                setResult(4, $this.getIntent());
                                finish();
                            }
                        });
                    } else {
                        Utils.showToast($this, "请签署优家库设计合同");
                        if (requireData != null && !TextUtils.isEmpty(requireData.getContractCode()) && !TextUtils.isEmpty(requireData.getUserCode())) {
                            String param = "contractCode=" + requireData.getContractCode() + "&signer=" + requireData.getUserCode();
                            startActivityForResult(new Intent($this, WebActivity.class)
                                            .putExtra(KEY_TYPE, WEB_TYPE_CONTRACT)
                                            .putExtra(KEY_PARAM, param)
                                    , 2);
                        }else {
                            if(requireData == null){
                                Utils.showToast($this,"requireData == null");
                            }

                            if(TextUtils.isEmpty(requireData.getContractCode())){
                                Utils.showToast($this,"TextUtils.isEmpty(requireData.getContractCode()");
                            }

                            if(TextUtils.isEmpty(requireData.getUserCode())){
                                Utils.showToast($this,"TextUtils.isEmpty(requireData.getUserCode())");
                            }
                        }
                    }
                }
                break;
            case R.id.txtContract:
            case R.id.checkBox:

                if (!canCheck) {
                    checkBox.setChecked(false);
                    if (requireData != null && !TextUtils.isEmpty(requireData.getContractCode()) && !TextUtils.isEmpty(requireData.getUserCode())) {
                        String param = "contractCode=" + requireData.getContractCode() + "&signer=" + requireData.getUserCode();
                        startActivityForResult(new Intent($this, WebActivity.class)
                                        .putExtra(KEY_TYPE, WEB_TYPE_CONTRACT)
                                        .putExtra(KEY_PARAM, param)
                                , 2);
                    }else {
                        if(requireData == null){
                            Utils.showToast($this,"requireData == null");
                        }

                        if(TextUtils.isEmpty(requireData.getContractCode())){
                            Utils.showToast($this,"TextUtils.isEmpty(requireData.getContractCode()");
                        }

                        if(TextUtils.isEmpty(requireData.getUserCode())){
                            Utils.showToast($this,"TextUtils.isEmpty(requireData.getUserCode())");
                        }
                    }
                } else {
                    checkBox.setChecked(true);
                    txtContractSigned.setVisibility(View.VISIBLE);
                    txtContract.setVisibility(View.GONE);
                }

                break;
        }
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            setResult(4, this.getIntent());
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        if (requestCode == 1 && resultCode == RESULT_OK) {
            //成功  关闭
            setResult(RESULT_OK);
            setResult(4, getIntent());
            finish();
        }

        if (requestCode == 2 && resultCode == RESULT_OK) {
//            ContractSaveRequest bean = new ContractSaveRequest();
//            //data.getUserRequireId(),requireData.getContractCode(),requireData.getUserCode()
//            bean.setSigner(requireData.getUserCode());
//            bean.setContractCode(requireData.getContractCode());
//            bean.setToken(ConfigValue.DATA_KEY);
//            bean.setUserRequireId(data.getUserRequireId());
            Map<String, String> param = new HashMap<>();
            param.put(KEY_TOKEN, ConfigValue.DATA_KEY);
            param.put("contractCode", requireData.getContractCode());
            param.put("requireId", String.valueOf(data.getUserRequireId()));
            presenter.saveContractForPhoneDesign($this, param, new BaseView.OnLoadListener() {
                @Override
                public void onLoad(Object content) {
                    canCheck = true;
                    checkBox.setChecked(true);
                    txtContractSigned.setVisibility(View.VISIBLE);
                    txtContract.setVisibility(View.GONE);
                }
            });
        }
    }
}
